<%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<c:set var="basePath" value="${pageContext.request.contextPath}"/>
<%--<c:set var="upmsAgents" value="${upmsAgents}"/>--%>

<div id="createDialog" class="container-fluid">
    <div class="row">
        <form id="createForm" method="post" class="form-horizontal" action="">
            <div class="form-group">
                <label class="col-sm-4 control-label" for="name">
                    <span class="help-inline"><font color="red">*</font> </span>
                    名称：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="name" name="name" placeholder="名称" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="code">
                    <span class="help-inline"><font color="red">*</font> </span>
                    机器码：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="code" name="code" placeholder="机器码" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="pathcount">
                    <span class="help-inline"><font color="red">*</font> </span>
                    伞道数：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="pathcount" name="pathcount" placeholder="伞道数" maxlength="2" value="1" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="capacity">
                    <span class="help-inline"><font color="red">*</font> </span>
                    额定容量：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="capacity" name="capacity" placeholder="额定容量" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="type">
                    <span class="help-inline"><font color="red">*</font> </span>
                    类型：</label>
                <div class="col-sm-5">
                    <select id="type" name="type" class="form-control" >
                        <c:forEach var="machineType" items="${machineTypeList}">
                            <option value="${machineType.dicValue}">${machineType.dicText}</option>
                        </c:forEach>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="status">
                    <span class="help-inline"><font color="red">*</font> </span>
                    状态：</label>
                <div class="col-sm-5">
                    <select id="status" name="status" class="form-control" placeholder="状态">
                        <c:forEach var="upmsDict" items="${statusList}">
                            <option value="${upmsDict.dicValue}" <c:if test="${umbrellaHardwareMachine.status==upmsDict.dicValue}">selected="selected"</c:if>>${upmsDict.dicText}</option>
                        </c:forEach>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="rentRuleId">
                    <span class="help-inline"><font color="red">*</font> </span>
                    租赁规则：</label>
                <div class="col-sm-5">
                    <select id="rentRuleId" name="rentRuleId" class="form-control" >
                        <c:forEach var="ruleRent" items="${ruleRentList}">
                            <option value="${ruleRent.id}">${ruleRent.name}</option>
                        </c:forEach>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="ruleDepositId">
                    <span class="help-inline"><font color="red">*</font> </span>
                    预付金规则：</label>
                <div class="col-sm-5">
                    <select id="ruleDepositId" name="ruleDepositId" class="form-control" >
                        <c:forEach var="deposit" items="${depositList}">
                            <option value="${deposit.id}">${deposit.name}</option>
                        </c:forEach>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="launchtime">
                    <span class="help-inline"><font color="red">*</font> </span>
                    投放时间：</label>
                <div class="col-sm-5">
                    <input type="text" id="launchtime" class="form-control" data-date-format="yyyy-mm-dd" placeholder="投放时间">
                    <input type="text" hidden name="launchtime" >
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="country">
                    <span class="help-inline"><font color="red">*</font> </span>
                    投放国家：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="country" name="country" placeholder="投放国家" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="province">
                    <span class="help-inline"><font color="red">*</font> </span>
                    投放省份：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="province" name="province" placeholder="投放省份" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="city">
                    <span class="help-inline"><font color="red">*</font> </span>
                    投放地市：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="city" name="city" placeholder="投放地市" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="address">
                    <span class="help-inline"><font color="red">*</font> </span>
                    投放详细地址：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="address" name="address" placeholder="投放详细地址" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="longitude">
                    <span class="help-inline"><font color="red">*</font> </span>
                    经度：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="longitude" name="longitude" placeholder="经度(GCJ02)" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="latitude">
                    <span class="help-inline"><font color="red">*</font> </span>
                    纬度：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="latitude" name="latitude" placeholder="纬度(GCJ02)" />
                </div>
            </div>
            <div class="form-group">
                <%--<label class="col-sm-4 control-label" for="description">点击地图获取坐标</label>--%>
                <%--<div class="col-sm-5">--%>
                    <%--<input type="text" class="form-control" id="searchmap" placeholder="输入关键字搜索地图位置。" />--%>
                <%--</div>--%>
                <%--<div class="col-sm-12   ">--%>
                    <%--<div id="l-map"></div>--%>
                <%--</div>--%>
                    <div class="col-sm-8 col-sm-offset-4">
                        <button type="button" class="btn btn-primary" onclick="openMap();">打开地图获取坐标</button>
                    </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="description">描述：</label>
                <div class="col-sm-5">
                    <textarea class="form-control" id="description" name="description" placeholder="描述"></textarea>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-9 col-sm-offset-4">
                    <button type="button" class="btn btn-primary" onclick="createSubmit();">
                        保存
                    </button>
                    <button type="button" class="btn btn-default"
                            onclick="createDialog.close();">取消
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>

<script type="text/javascript">
    /*
     // 百度地图API功能
     var map = new BMap.Map("l-map", {enableMapClick:false});            // 创建Map实例
     map.centerAndZoom(new BMap.Point(116.404, 39.915), 11);
     var local = new BMap.LocalSearch(map, {
         renderOptions: {map: map, panel: "r-result"}
     });
     local.search("北京市");
     $("#searchmap").change(function(){
         local.search($(this).val());
     });
     setTimeout(function(){
         map.setZoom(14);
     }, 2000);  //2秒后放大到14级
     map.enableScrollWheelZoom(true);
     //单击获取点击的经纬度
     map.addEventListener("click",function(e){
         var point = e.point;
         $.MapConvert.Convert_BD09_To_GCJ02(point);
         $("#latitude").val(point.lat);
         $("#longitude").val(point.lng);
     });
     */
</script>
<script>

    function validateForm(){
        return $( "#createForm" ).validate( {
            rules: {
                code: {
                    required: true,
                    maxlength: 50
                },
                pathcount: {
                    required: true,
                    maxlength: 2
                },
                name: {
                    required: true,
                    maxlength: 50
                },
                capacity: {
                    required: true,
                },
                type: {
                    required: true,
                },
                status: {
                    required: true,
                },
                launchtime: {
                },
                country: {
                },
                province: {
                },
                city: {
                },
                address: {
                },
                longitude: {
                    required: true,
                },
                latitude: {
                    required: true,
                },
                description: {
                    maxlength: 500
                }
            },
            messages: {
                code: {
                    required: "此项为必填项。",
                    maxlength: "长度超出50个字符"
                },
                pathcount: {
                    required: "此项为必填项。",
                    maxlength: "长度超出2个数字"
                },
                name: {
                    required: "此项为必填项。",
                    maxlength: "长度超出50个字符"
                },
                capacity: {
                    required: "此项为必填项。",
                },
                type: {
                    required: "此项为必填项。",
                },
                status: {
                    required: "此项为必填项。",
                },
                launchtime: {
                },
                country: {
                },
                province: {
                },
                city: {
                },
                address: {
                },
                longitude: {
                    required: "此项为必填项。",
                },
                latitude: {
                    required: "此项为必填项。",
                },
                description: {
                    maxlength: "描述长度超出500个字符"
                }
            },
            errorElement: "em",
            errorPlacement: function ( error, element ) {
                // Add the `help-block` class to the error element
                error.addClass( "help-block" );

                if ( element.prop( "type" ) === "checkbox" ) {
                    error.insertAfter( element.parent( "label" ) );
                } else {
                    error.insertAfter( element );
                }
            },
            highlight: function ( element, errorClass, validClass ) {
                $( element ).parents( ".col-sm-5" ).addClass( "has-error" ).removeClass( "has-success" );
            },
            unhighlight: function (element, errorClass, validClass) {
                $( element ).parents( ".col-sm-5" ).addClass( "has-success" ).removeClass( "has-error" );
            }
        } );
    }
    //注册表单验证
    $(validateForm());
    $(function(){
        $('#launchtime').datepicker({
//            format: 'yyyy-mm-dd'
        });
    });
    <%--$(function(){--%>
        <%--//初始化树形select控件--%>
        <%--$("#agent").bootstrapCombotree({--%>
            <%--data: ${upmsAgents},//data应符合实例的data格式--%>
            <%--defaultLable:"请选择",--%>
            <%--name : 'pid'//combotree值域的name，可以用在表单提交--%>
        <%--})--%>
    <%--});--%>

    function createSubmit() {
        if(validateForm().form()){
            if($("#launchtime").val())
                $("input[name='launchtime']").val(getLongFromatDateStr($("#launchtime").val()));
            $.ajax({dataType: 'json',
                type: 'post',
                url: '${basePath}/manage/machine/create',
                data: $('#createForm').serialize(),
                success: function(result) {
                    if (result.code != 1) {
                        if (result.data instanceof Array) {
                            $.each(result.data, function(index, value) {
                                infoDialog(value.errorMsg);
                            });
                        } else {
                            infoDialog(result.data);
                        }
                    } else {
                        createDialog.close();
                        $table.bootstrapTable('refresh');
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    infoDialog(textStatus);
                }
            });
        }
    }
</script>